<?php

namespace App\Controller;

use App\Exception\ImageParsingError;
use App\Form\ImagesSourceType;
use App\Model\ImagesSource;
use App\Service\ImageInfoProviderInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class ImagesInfoController extends AbstractController
{
    public function __construct(
        private readonly ImageInfoProviderInterface $imageInfoProvider
    ) {
    }

    #[Route('/', name: 'app_images_info')]
    public function index(Request $request): Response
    {
        $form = $this->createForm(ImagesSourceType::class, new ImagesSource(),
            [
                'method' => 'GET'
            ]
        );

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $url = ($form->getData()->getUrl());
            $imagesInfo = $this->imageInfoProvider->getImagesInfo($url);

            return $this->render('images_info/result.html.twig', [
                'imagesInfo' => $imagesInfo,
            ]);
        }

        return $this->render('images_info/index.html.twig', [
            'form' => $form->createView(),
        ]);
    }
}
